<script setup>
import { ref } from 'vue'
import { updateMyInformation } from '@/api/MyselfApi'
import { useMyInformationPinia } from '@/stores/models/useMyInformation'
import xuanRanInformation from '@/utils/xuanRanMyInformation'

const myInformationPinia = useMyInformationPinia()
// ........................

// 定义变量
// 获得昵称输入框
const myName = ref(null)
const myEMall = ref(null)
const username = ref(null)
// 获得校验框
const nicknameCheck = ref('')
const eMallCheck = ref('')
// 是否显示校验
const isShowNicknameCheck = ref(false)
const isShowEMallCheck = ref(false)
// 加载
const loading = ref(false)
// 装个人信息的盒子
const myInformationBox = ref(myInformationPinia.myInformation)

// .........................

// 一进页面渲染
const fun = async () => {
  myInformationBox.value = await xuanRanInformation()
  username.value = myInformationBox.value.username
}
fun()

// ........................

// 封装校验框
const updateCheck = (name,string) => {
  if (name === 'myName'){
    // 给昵称校验
    nicknameCheck.value.innerText = string
    isShowNicknameCheck.value = true
    setTimeout(() => {
      nicknameCheck.value.innerText = ''
      isShowNicknameCheck.value = false
    },5000)
  } else {
    // 给邮箱校验
    eMallCheck.value.innerText = string
    isShowEMallCheck.value = true
    setTimeout(() => {
      eMallCheck.value.innerText = ''
      isShowEMallCheck.value = false
    },5000)
  }
}

// ........................

// 提交
const submit = async () => {
  if (myName.value.value === ''){
    updateCheck('myName','昵称不能为空')
  }else if (myEMall.value.value === ''){
    updateCheck('myEMall','邮箱不能为空')
    return
  }else if (myName.value.value.length >10 || /\s/.test(myName.value.value)){
    updateCheck('myName','昵称不符合规范')
  }else if (!/^[a-zA-Z0-9._%+-]+@(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}(?:\.[a-zA-Z]{2,})?$/.test(myEMall.value.value)){
    updateCheck('myEMall','邮箱不符合规范')
  }else {
    loading.value = true
    myInformationBox.value = await updateMyInformation(myInformationPinia.myInformation.id,myName.value.value,myEMall.value.value)
    nicknameCheck.value.innerText = ''
    eMallCheck.value.innerText = ''
    loading.value = false
    // xuanRanInformation()
    history.go(0)
  }
  
}

</script>

<template>
  <div class="maxBox"
    v-loading="loading"
    element-loading-text="拼命加载中..."
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.7)"
  >
    <div class="top">
      <div class="title">基本资料</div>
    </div>
    <div class="foot">
      <div class="inps">
        <span>用户账号</span>
        <input type="text" class="inp" disabled :value="username">
      </div>
      <div class="inps">
        <span>用户昵称</span>
        <input type="text" class="inp" ref="myName">
        <div class="jiaoYan" ref="nicknameCheck" v-show="isShowNicknameCheck">我是校验框</div>
      </div>
      <div class="inps">
        <span>用户邮箱</span>
        <input type="text" class="inp" ref="myEMall">
        <div class="jiaoYan" ref="eMallCheck" v-show="isShowEMallCheck">我是校验框</div>
      </div>
      <button class="btn tiJiao" @click="submit">提交修改</button>
    </div>
  </div>

</template>

<style scoped>
  * {
    padding: 0;
    margin: 0;
    box-sizing: border-box;
    /* border: 1px solid red; */
  }
  .maxBox {
    width: 980px;
    min-height: 1000px;
    /* background-color: rgb(255, 255, 255); */
    /* box-shadow: -2px -2px 5px rgb(210, 208, 208); */
  }
  /* top样式开始 */
  .top {
    width: 100%;
    height: 80px;
    /* position: relative; */
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .title {
    width: 200px;
    height: 100%;
    text-align: center;
    line-height: 80px;
    font-weight: 700;
    color: black;
    font-size: 40px;
  }
  /* top样式结束 */
  .foot {
    width: 100%;
  }
  /* 输入框加文本加校验框开始 */
  .inps {
    height: 80px;
    width: 500px;
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
  }
  .inp {
    width: 350px;
    height: 35px;
    border-radius: 5px;
    background-color: transparent;
  }
  .inp:focus {
    outline: none; /* 移除默认的聚焦轮廓 */
    border-color: #74b4e9; /* 设置蓝色边框 */
    transition: all 0.3s ease; /* 添加过渡动画 */
  }
  .inps span {
    display: block;
    width: 100px;
    height: 100%;
    text-align: center;
    line-height: 80px;
    font-size: 18px;
  }
  .jiaoYan {
    width: 200px;
    height: 20px;
    /* background-color: aqua; */
    overflow: hidden;
    position: absolute;
    bottom: 1px;
    left: 125px;
    text-align: 20px;
    font-size: 10px;
    color: red;
  }
  /* 输入框加文本加校验框结束 */
  .tiJiao {
    margin-left: 160px;
  }
</style>